import { mapGetters, mapActions } from "vuex";
import _ from "lodash";
import { websocketsend } from "@/assets/js/websocket";
import CheckIcon from "vux/src/components/check-icon/index.vue";

import { constants } from "zlib";
import { timeFormat } from "@/assets/js/dom";

import template from "./niuniuTemplate";
const { render, staticRenderFns } = template;

export default {
  render,
  staticRenderFns,
  props: ["oddsData", "betMoneyList", "lDragonData"],
  data() {
    return {
      showBetInfoBox: false,
      showBetOpBox: false,
      showBetInfoList: false,
      methodsArr: [], //玩法列表
      chekedMethod: 0, //选中的玩法下标
      playMethod: {}, //选中的玩法
      showSelect: false, //是否有下拉列表
      checkedOddIndex: -1, //选中赔率下标
      checkedOdd: [], //选中的赔率
      zhuDanOdd: [], //注单arr
      checkedOddLen: [], //选中的赔率长度
      oddsWay: [], //投注数据
      moneyArr: [], //金额数组
      totalMoney: 0, //选中总金额
      totalMoney1: 0, //注单总金额
      isZhuiHao: false, //是否是追号
      checkedMoneyIdx: 0, //选中筹码下标
      checkedMoney: 0, //选中金额
      yqMoney: 0, //预期盈利
      maxyqMoney: 0, //预期最大盈利
      minyqMoney: 0, //预期最小盈利
      manyBei: 1,
      manyQi: 1,
      totalZhMoney: 0,
      showZhBox: false, //显示追号
      showZhList: false, //显示追号list
      zhName: "", //追号名称
      zhuiHaoList: [],
      zhBetData: [],
      checkedName: "",
      winStop: true, //是否追停
      showOddsTip: false,
      activeSetect: "", //选中的select
      token: localStorage.getItem("token"),
      descDetail: "",
    };
  },

  created() {},
  mounted() {},
  computed: {
    ...mapGetters(["roomData", "userInfo", "accountData", "serTime"]),
  },
  methods: {
    hideBetOpBox: function () {
      this.showBetOpBox = false;
      this.showBetInfoBox = true;
    },
    checkOdds: function (index, title, isNum, fjIndex) {
      //选中赔率
      if (fjIndex == undefined) {
        fjIndex = 0;
      }
      var newIndex = index + "_" + fjIndex;
      var value = _.isArray(this.playMethod[fjIndex])
        ? this.playMethod[fjIndex][index].value
        : this.playMethod[index].data[0].value;
      var name = _.isArray(this.playMethod[fjIndex])
        ? this.playMethod[fjIndex][index].title
        : this.playMethod[index].title;
      if (this.methodsArr[this.chekedMethod].name == "长龙投注") {
        value = this.playMethod[fjIndex].data[index].odds;
        name = this.playMethod[fjIndex].data[index].way;
      }
      if (!isNum) {
        this.descDetail =
          this.playMethod[index].desc || this.playMethod[index].data[0].desc;
      }
      if (isNum && this.roomData.notZhuiHao) {
        var arr = JSON.parse(JSON.stringify(this.checkedOdd));
        if (this.oddsWay.length < 1) {
          this.checkedOdd.push({
            money: this.checkedMoney,
            index: newIndex,
            value: value,
            name: name,
          });
          this.oddsWay.push(newIndex);
          this.checkedOddLen = this.checkedOdd.length;
          this.checkedOddIndex = newIndex;
          this.totalMoney = this.checkedOdd.length * this.checkedMoney;
          return;
        }
        var flag = false;
        var arrIndex = 0;
        for (var i = 0; i < this.oddsWay.length; i++) {
          if (this.oddsWay[i] != newIndex) {
            flag = true;
          } else {
            flag = false;
            arrIndex = i;
            break;
          }
        }
        if (flag) {
          this.checkedOddIndex = newIndex;
          this.checkedOdd.push({
            money: this.checkedMoney,
            index: newIndex,
            value: value,
            name: name,
          });
          this.oddsWay.push(newIndex);
          this.totalMoney = this.checkedOdd.length * this.checkedMoney;
        } else {
          this.checkedOddIndex = -1;
          this.oddsWay.splice(arrIndex, 1);
          this.checkedOdd.splice(arrIndex, 1);
          this.totalMoney = this.checkedOdd.length * this.checkedMoney;
        }
        this.checkedOddLen = this.checkedOdd.length;
        console.log(this.oddsWay, "this.oddsWay");
        return;
      }
      if (this.checkedOddIndex == newIndex) {
        this.checkedOddIndex = -1;
        this.checkedOddLen = 0;
        this.checkedOdd = [];
        this.oddsWay = [];

        this.descDetail = "";
      } else {
        this.checkedOddIndex = newIndex;
        this.checkedOdd = [
          {
            value: value,
            money: this.checkedMoney,
            index: newIndex,
            name: name,
          },
        ];
        console.log(this.checkedOdd, "this.checkedOdd");
        this.oddsWay = [newIndex];
        this.totalMoney = this.checkedOdd.length * this.checkedMoney;
        this.checkedOddLen = 1;
      }
    },
    changeSltIdx: function (val) {
      //切换select
      if (this.$refs.scroll) {
        var arr = this.methodsArr[this.chekedMethod].list;
        for (var i = 0; i < arr.length; i++) {
          if (arr[i] == val[0]) {
            let activeLocation = document.getElementById("pmTitle" + i);
            this.$refs.scroll.scrollTo(0, -activeLocation.offsetTop, 500);
          }
        }
      }
      this.activeSetect = val[0];
      return;
    },
    setZhDlg: function () {
      var totalZhMoney = 0;
      var zhTableData = [];
      var zhBetData = [];
      var issue = this.roomData.issue;
      this.manyQi = Math.round(this.manyQi);
      this.manyBei = Math.round(this.manyBei);
      if (this.manyQi > 100) {
        this.$vux.alert.show({ content: "最多追加100期！" });
        this.manyQi = 100;
        return;
      }
      for (var i = 1; i <= this.manyQi; i++) {
        totalZhMoney += this.checkedMoney * Math.pow(this.manyBei, i - 1);
        zhTableData.push({
          issue: issue,
          money: this.checkedMoney * Math.pow(this.manyBei, i - 1) + "元宝",
          multiple: Math.pow(this.manyBei, i - 1) + "倍",
        });
        zhBetData.push({
          qihao: issue,
          money: this.checkedMoney * Math.pow(this.manyBei, i - 1),
          single_money: this.checkedMoney,
          way: this.checkedOdd[0].name,
          multiple: Math.pow(this.manyBei, i - 1),
        });
        issue++;
      }
      this.zhuiHaoList = zhTableData;
      this.totalZhMoney = totalZhMoney;
      this.zhBetData = zhBetData;
      this.showZhList = true;
    },
    sendZhOdds: function () {
      //发送追号
      var that = this;
      this.$vux.confirm.show({
        title: "提示",
        content:
          "本次追号" + that.manyQi + "期，共" + that.totalZhMoney + "元宝",
        onConfirm: function () {
          const agentData = {
            token: that.token,
            commandid: "3019",
            nickname: that.userInfo.nickname,
            data: that.zhBetData,
            win_stop: that.winStop ? 1 : 0,
            avatar: that.userInfo.avatar,
          };
          websocketsend(agentData);
          var obj = that.roomData;
          obj.showPmDlg = false;
          that.$store.commit("setRoomData", obj);
        },
      });
    },
    sendOdds: function () {
      //投注
      if (this.checkedOdd.length < 1) {
        return;
      }
      if (this.roomData.stopBet) {
        this.$vux.alert.show({ content: "当前已封盘，不能投注" });
        return;
      }
      var way = [];
      var money = [];
      for (var i = 0; i < this.checkedOdd.length; i++) {
        way.push(this.checkedOdd[i].name);
        money.push(this.checkedOdd[i].money);
      }
      const agentData = {
        token: this.token,
        commandid: "3006",
        nickname: this.userInfo.nickname,
        way: way,
        money: money,
        avatar: this.userInfo.avatar,
      };
      var uniNo = new Date().getTime() + Math.floor(Math.random() * 900) + 100;
      var params = {
        user_id: this.userInfo.id,
        lottery_type: this.$route.query.lottery_type,
        room_id: this.$route.query.id,
        nickname: this.userInfo.nickname,
        money: JSON.stringify(money),
        issue: this.roomData.issue,
        way: JSON.stringify(way),
        serTime: this.serTime,
        uniNo: uniNo,
      };
      let lotRstData = {
        isHttp: true,
        isLoading: true,
        avatar: this.userInfo.avatar,
        commandid: 30007,
        issue: this.roomData.issue,
        money: money,
        nickname: this.userInfo.nickname,
        time: timeFormat(new Date().getTime()),
        total_money: this.totalMoney,
        total_zushu: this.checkedOdd.length,
        uid: this.userInfo.id,
        uniNo: uniNo,
        sort: this.userInfo.honor.sort,
        serTime: this.serTime,
        order_no: "",
        way: way,
        loadingFailed: false,
        failedText: "网络错误，请重试",
      };
      this.$store.commit("setLotRstData", lotRstData);
      this.$http
        .post(this.urlRequest + "?m=api&c=workerman&a=http_betting", params)
        .then(
          (result) => {
            if (result.commandid != 3007) {
              var failedObj = JSON.parse(JSON.stringify(lotRstData));
              failedObj.loadingFailed = true;
              failedObj.failedText = result.content;
              this.$store.commit("setNewRstData", failedObj);
              return;
            }
            var accountData = this.accountData;
            accountData.money_usable = result.current_money;
            this.$store.commit("setAccountData", accountData);
            this.$store.commit("setNewRstData", result);
          },
          (err) => {
            lotRstData.loadingFailed = true;
            lotRstData.failedText = "投注超时，请重新下注";
            this.$store.commit("setNewRstData", lotRstData);
          }
        );

      var obj = this.roomData;
      obj.showPmDlg = false;
      this.$store.commit("setRoomData", obj);
      this.checkedOdd = [];
      this.oddsWay = [];
    },
    sendZdOdds: function () {
      //注单投注
      if (this.zhuDanOdd.length < 1) {
        return;
      }
      if (this.roomData.stopBet) {
        this.$vux.alert.show({ content: "当前已封盘，不能投注" });
        return;
      }
      var that = this;
      var way = [];
      var money = [];
      for (var i = 0; i < this.zhuDanOdd.length; i++) {
        way.push(this.zhuDanOdd[i].name);
        money.push(this.zhuDanOdd[i].money);
      }
      this.$vux.confirm.show({
        title: "提示",
        content: "本次投注" + that.totalMoney1 + "元宝",
        onConfirm: () => {
          const agentData = {
            token: that.token,
            commandid: "3006",
            nickname: that.userInfo.nickname,
            way: way,
            money: money,
            avatar: that.userInfo.avatar,
          };
          var uniNo =
            new Date().getTime() + Math.floor(Math.random() * 900) + 100;
          var params = {
            user_id: this.userInfo.id,
            lottery_type: this.$route.query.lottery_type,
            room_id: this.$route.query.id,
            nickname: this.userInfo.nickname,
            money: JSON.stringify(money),
            issue: this.roomData.issue,
            way: JSON.stringify(way),
            serTime: this.serTime,
            uniNo: uniNo,
          };
          let lotRstData = {
            isHttp: true,
            isLoading: true,
            avatar: this.userInfo.avatar,
            commandid: 30007,
            issue: this.roomData.issue,
            money: money,
            nickname: this.userInfo.nickname,
            time: timeFormat(new Date().getTime()),
            total_money: this.totalMoney,
            total_zushu: this.zhuDanOdd.length,
            uid: this.userInfo.id,
            uniNo: uniNo,
            sort: this.userInfo.honor.sort,
            serTime: this.serTime,
            order_no: "",
            way: way,
            loadingFailed: false,
            failedText: "网络错误，请重试",
          };
          this.$store.commit("setLotRstData", lotRstData);
          this.$http
            .post(this.urlRequest + "?m=api&c=workerman&a=http_betting", params)
            .then(
              (result) => {
                if (result.commandid != 3007) {
                  var failedObj = JSON.parse(JSON.stringify(lotRstData));
                  failedObj.loadingFailed = true;
                  failedObj.failedText = result.content;
                  this.$store.commit("setNewRstData", failedObj);
                  return;
                }
                var accountData = this.accountData;
                accountData.money_usable = result.current_money;
                this.$store.commit("setAccountData", accountData);
                this.$store.commit("setNewRstData", result);
              },
              (err) => {
                lotRstData.loadingFailed = true;
                lotRstData.failedText = "投注超时，请重新下注";
                this.$store.commit("setNewRstData", lotRstData);
              }
            );

          var obj = that.roomData;
          obj.showPmDlg = false;
          that.$store.commit("setRoomData", obj);
          that.checkedOdd = [];
          that.zhuDanOdd = [];
        },
      });
    },
    addOddsList: function () {
      //加入注单
      this.zhuDanOdd = this.zhuDanOdd.concat(this.checkedOdd);
      this.oddsWay = [];
      this.checkedOdd = [];
      this.checkedOddIndex = -1;
      this.showBetInfoList = false;
      this.showBetOpBox = false;
      this.showBetInfoBox = true;
    },
    clearBetList: function () {
      //清空注单
      var that = this;
      this.$vux.confirm.show({
        title: "提示",
        content: "是否清空当前注单？",
        onConfirm: function () {
          that.zhuDanOdd = [];
        },
      });
    },
    removeBetItem: function (index) {
      // 清空单注
      this.zhuDanOdd.splice(index, 1);
    },
    setMinMoney: function () {
      this.checkedMoney = this.roomData.minBetMoney;
      this.checkedMoneyIdx = -1;
    },
    checkedOddsStyle: function (index, isNum, fjIndex) {
      //选中样式绑定
      if (fjIndex == undefined) {
        fjIndex = 0;
      }
      index = index + "_" + fjIndex;
      if (isNum) {
        for (var i = 0; i < this.oddsWay.length; i++) {
          if (this.oddsWay[i] == index) {
            return true;
          }
        }
      }
      if (this.checkedOddIndex == index) {
        return true;
      }
    },
    checkMoney: function (index, item) {
      if (item == "梭哈") {
        let accountData = this.accountData.money_usable;
        this.checkedMoney = Math.round(Number(accountData));
        this.checkedMoneyIdx = index;
        this.totalMoney = this.checkedMoney * this.checkedOdd.length;
      } else {
        this.checkedMoney = item;
        this.checkedMoneyIdx = index;
      }
      for (var i = 0; i < this.checkedOdd.length; i++) {
        if (this.checkedOdd[i].index == this.checkedOddIndex) {
          this.checkedOdd[i].money = this.checkedMoney;
        }
      }
    },
    changePlay: function (id) {
      //换玩法
      if (!_.isEmpty(this.methodsArr[id].list)) {
        //如果是有下拉的
        var firstSelect = this.methodsArr[id].list[0];
        this.playMethod = this.methodsArr[id].value;
        console.log(this.playMethod, "playMethod");
        this.showSelect = true;
        this.activeSetect = firstSelect;
      } else {
        this.playMethod = this.methodsArr[id].value;
        this.showSelect = false;
      }
      this.chekedMethod = id;

      this.descDetail = "";
      this.resetOdds();
    },
    resetOdds() {
      this.oddsWay = [];
      this.checkedOddIndex = -1;
      this.showZhBox = false;
      this.checkedOdd = [];
      this.zhuiHaoList = [];
    },
    showZhBoxMask() {
      this.checkedOdd = [];
      this.checkedOddIndex = -1;
      this.oddsWay = [];
      this.showZhBox = false;
    },
  },
  components: {
    CheckIcon,
  },
  watch: {
    oddsData: function (newVal, oldVal) {
      //赔率信息
      var arr1 = [];
      if (!_.isEmpty(newVal["panel_8"])) {
        arr1.push({
          key: "panel_8",
          name: "猜胜负",
          value: newVal["panel_8"],
        });
      }
      if (!_.isEmpty(newVal["panel_1"])) {
        arr1.push({
          key: "panel_1",
          name: "猜牛牛",
          value: newVal["panel_1"],
        });
      }
      if (!_.isEmpty(newVal["panel_2"])) {
        var list = ["第一张", "第二张", "第三张", "第四张", "第五张"];
        var value = [];
        for (var i = 0; i < list.length; i++) {
          value.push(newVal["panel_2"][i].data);
        }
        arr1.push({
          key: "panel_2",
          name: "猜牌面",
          value: value,
          list: list,
        });
      }
      if (!_.isEmpty(newVal["panel_3"])) {
        var list = ["第一张", "第二张", "第三张", "第四张", "第五张"];
        var value = [];
        for (var i = 0; i < list.length; i++) {
          value.push(newVal["panel_3"][i].data);
        }
        arr1.push({
          key: "panel_3",
          name: "猜双面",
          value: value,
          list: list,
        });
      }
      if (!_.isEmpty(newVal["panel_4"])) {
        var list = ["第一张", "第二张", "第三张", "第四张", "第五张"];
        var value = [];
        for (var i = 0; i < list.length; i++) {
          value.push(newVal["panel_4"][i].data);
        }
        arr1.push({
          key: "panel_4",
          name: "猜花色",
          value: value,
          list: list,
        });
      }
      if (!_.isEmpty(newVal["panel_5"])) {
        arr1.push({
          key: "panel_5",
          name: "猜龙虎",
          value: newVal["panel_5"],
        });
      }
      if (!_.isEmpty(newVal["panel_6"])) {
        arr1.push({
          key: "panel_6",
          name: "猜公牌",
          value: newVal["panel_6"],
        });
      }
      if (!_.isEmpty(newVal["panel_7"])) {
        arr1.push({
          key: "panel_7",
          name: "猜总和",
          value: newVal["panel_7"],
        });
      }
      this.methodsArr = arr1;
      this.showSelect = false;
      this.activeSetect = "";
      this.playMethod = arr1[0].value;
    },
    lDragonData: function (newVal, oldVal) {
      if (newVal) {
        for (var i = 0; i < this.methodsArr.length; i++) {
          if (this.methodsArr[i].name == "长龙投注") {
            this.methodsArr.splice(i, 1);
          }
        }
        if (newVal.length > 0) {
          this.methodsArr.push({
            key: "长龙投注",
            name: "长龙投注",
            value: newVal,
          });
          if (this.methodsArr[this.chekedMethod].name == "长龙投注") {
            this.playMethod = newVal;
          }
        }
      }
    },
    checkedOddIndex: function (newVal) {},
    betMoneyList: function (newVal) {
      this.checkedMoney = this.betMoneyList[0];
    },
    checkedOdd: function (newVal) {
      var yqMoneyArr = [];
      for (var i = 0; i < newVal.length; i++) {
        yqMoneyArr.push((newVal[i].money * newVal[i].value).toFixed(0));
      }
      this.maxyqMoney = Math.max.apply(null, yqMoneyArr);
      this.minyqMoney = Math.min.apply(null, yqMoneyArr);

      if (this.roomData.notZhuiHao) {
        if (newVal.length > 0) {
          this.showBetOpBox = true;
          this.showBetInfoBox = false;
        } else {
          this.showBetInfoBox = true;
          this.showBetOpBox = false;
          this.minyqMoney = 0;
        }
      } else {
        if (newVal.length > 0) {
          this.showZhBox = true;
          this.zhName = newVal[newVal.length - 1].name;
        } else {
          this.showZhBox = false;
        }
      }
    },
    zhuDanOdd: function (newVal) {
      this.totalMoney1 = 0;
      var yqMoneyArr = [];
      if (newVal.length > 0) {
        for (var i = 0; i < newVal.length; i++) {
          this.totalMoney1 += parseInt(newVal[i].money);
          yqMoneyArr.push((newVal[i].money * newVal[i].value).toFixed(0));
        }
        this.maxyqMoney = Math.max.apply(null, yqMoneyArr);
        this.minyqMoney = Math.min.apply(null, yqMoneyArr);
      } else {
        this.maxyqMoney = 0;
        this.minyqMoney = 0;
      }
    },
    checkedMoney: function (newVal) {
      var yqMoneyArr = [];
      this.totalMoney = newVal * this.checkedOdd.length;
      if (this.checkedOdd.length > 0) {
        for (var i = 0; i < this.checkedOdd.length; i++) {
          this.checkedOdd[i].money = newVal;
          yqMoneyArr.push(
            (this.checkedOdd[i].money * this.checkedOdd[i].value).toFixed(0)
          );
        }
        this.maxyqMoney = Math.max.apply(null, yqMoneyArr);
        this.minyqMoney = Math.min.apply(null, yqMoneyArr);
      }
    },
    roomData: {
      handler: function (newVal) {
        if (!newVal.showPmDlg) {
          this.showZhBox = false;
          this.showBetOpBox = false;
          this.showBetInfoBox = false;
          this.showZhList = false;
          this.resetOdds();
        }
        if (this.$refs.scroll) {
          this.$refs.scroll.refresh();
        }
        if (!this.notZhuiHao) {
          this.showBetOpBox = false;
          this.showBetInfoBox = false;
        }
        this.descDetail = "";
      },
      deep: true,
    },
  },
  deactivated() {},
  beforeDestroy() {},
};

// WEBPACK FOOTER //
// ./src/components/room/playMethods/niuniu/niuniu.vue
